package com.zxy.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.zxy.reggie.common.BaseContext;
import com.zxy.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 检查用户是否进行登录
 * @author 周向阳
 */
@WebFilter(filterName = "loginCheckFilter",urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
    /**
     *  路径匹配器，支持统配符
     */
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        log.info("拦截到请求:{}",request.getRequestURL());
//        1、获取本次请求url
//        定义不需要拦截的路径
        String requestURL = String.valueOf(request.getRequestURL());

        String[] urls = new String[]{
                "/employee/login",
                "/employee/login",
                "/backend/**",
                "/front/**",
                "/common/**",
                "/user/sendMsg",
                "/user/login"
        };
//        2、判断路径是否需要处理
        boolean check = check(urls,requestURL);
//        3、如果不需要处理直接放行
        if(check){
            log.info("本次请求不需要处理");

            filterChain.doFilter(request,response);

            return;
        }
//        4-1、判断员工登录，如果登录，放行
        if(request.getSession().getAttribute("employee") != null){
            log.info("用户已登录，用户ID为:{}",request.getSession().getAttribute("employee"));

            Long empid = (Long)request.getSession().getAttribute("employee");
            log.info("获取到的id: {}",empid);
            BaseContext.setCurrentId(empid);

            Long id = Thread.currentThread().getId();
            log.info("当前线程id:{}",id);

            filterChain.doFilter(request,response);
            return;
        }
//        4-2、判断用户登录，如果登录，放行
        if(request.getSession().getAttribute("user") != null){
            log.info("用户已登录，用户ID为:{}",request.getSession().getAttribute("user"));

            Long userid = Thread.currentThread().getId();
            log.info("当前线程id:{}",userid);

            Long empid = (Long)request.getSession().getAttribute("user");
            log.info("获取到的User_id: {}",empid);
            BaseContext.setCurrentId(empid);

            filterChain.doFilter(request,response);
            return;
        }
//        5、未登录，，通过输出流方式向前端传数据
        log.info("用户未登录");
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;
    }
    public boolean check(String[] urls,String requestURL){
        for (String url : urls){
            String url1 = "http://localhost:8080"+url;
            boolean match =  PATH_MATCHER.match(url1,requestURL);
            if (match){
                return true;
            }
        }
        return false;
    }
}
